package com.genersoft.iot.vmp.business;

import com.alibaba.fastjson.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

@Component
public class BizHttpService {

    private final static Logger logger = LoggerFactory.getLogger(BizHttpService.class);

    @Value("${business.remoteIp}")
    String remoteIp;

    @Value("${server.port}")
    int localServerPort;

    @Value("${business.ourBizIp}")
    String ourBizIp;

    @Autowired
    private RestTemplate mRestTemplate;

    public String alarmTransmitPost(MultiValueMap<String, String> map) {
        return transmitPost("/hb2/rest/public/hebei/handan/alarm", map);
    }

    public void onRecordMp4Transmit(JSONObject json) {
        transmitOurBizPost("/tower/api/updateCameraStateStopVideo.do", json);
    }

    public void onStreamChangedTransmit(JSONObject json) {
        transmitOurBizPost("/tower/api/updateCameraStatePull.do", json);
    }

    public String transmitOurBizPost(String url, JSONObject json) {
        if (ourBizIp == null || ourBizIp.isEmpty()) {
            return null;
        }
        String bizUrl = ourBizIp + url;
        HttpHeaders headers = new HttpHeaders();
        MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
        headers.setContentType(type);
        headers.add("Accept", MediaType.APPLICATION_JSON.toString());
        HttpEntity<String> formEntity = new HttpEntity<>(json.toString(), headers);
        try {
            mRestTemplate.postForEntity(bizUrl, formEntity, String.class).getBody();
        } catch (Exception e) {
            if (logger.isDebugEnabled()) {
                logger.debug("our biz post error:" + e.toString());
            }
        }

        return null;
    }

    public String transmitPost(String url, MultiValueMap<String, String> map) {
        if (remoteIp == null || remoteIp.isEmpty()) {
            return null;
        }
        String bizUrl = remoteIp + url;
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
        HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<>(map,
                headers);
        try {
            HttpEntity<String> responseEntity = mRestTemplate.postForEntity(bizUrl, request, String.class);
            return responseEntity.getBody();
        } catch (Exception e) {
            logger.info(String.format("biz transmit post url %s error %s", bizUrl, e.toString()));
        }

        return null;
    }

    public String executeLocalGetApi(String suffixUrl) {
        String url = "http://127.0.0.1:" + localServerPort + suffixUrl;
        HttpHeaders headers = new HttpHeaders();
        MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
        headers.setContentType(type);
        headers.add("Accept", MediaType.APPLICATION_JSON.toString());
        try {
            HttpEntity<String> responseEntity = mRestTemplate.getForEntity(url, String.class);
            return responseEntity.getBody();
        } catch(Exception e) {
            logger.info(String.format("execute local get api url %s error %s", url, e.toString()));
        }

        return null;
    }

    public String executeLocalPostApi(String suffixUrl, JSONObject json) {
        String url = "http://127.0.0.1:" + localServerPort + suffixUrl;
        HttpHeaders headers = new HttpHeaders();
        MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
        headers.setContentType(type);
        headers.add("Accept", MediaType.APPLICATION_JSON.toString());
        HttpEntity<String> formEntity = null;
        if (json != null) {
            formEntity = new HttpEntity<>(json.toString(), headers);
        }
        String result = null;
        try {
            result = mRestTemplate.postForEntity(url, formEntity, String.class).getBody();
        } catch (Exception e) {
            logger.info(String.format("execute local post api url %s error %s", url, e.toString()));
        }

        return result;
    }
}
